import { ApiProperty } from "@nestjs/swagger";
import { Column, Entity, Index, JoinColumn, ManyToOne, OneToOne, PrimaryGeneratedColumn } from "typeorm";
import { Goods } from "./Goods";
import { Unit } from "./Unit";

@Entity()
@Index(["goodsId", "unitId"], {unique: true})
export class Stock {

    constructor(id?:number, unitId?:number, goodsId?:number, count?:number) {
        this.id = id;
        this.unitId = unitId;
        this.goodsId = goodsId;
        this.count = count;
    }
    
    @ApiProperty({description: "主键ID"})
    @PrimaryGeneratedColumn()
    id: number;

    @ApiProperty({description: "单位ID"})
    @Column({comment: "单位ID", name: "unit_id"})
    unitId: number;
    
    @ApiProperty({description: "商品ID"})
    @Column({comment: "商品ID", name: "goods_id"})
    goodsId: number;

    @ApiProperty({description: "库存数量"})
    @Column({comment: "库存数量", default: 0})
    count: number;


    /**
     * 关联关系
     */
    @OneToOne(() => Unit, {createForeignKeyConstraints: false})
    @JoinColumn({name: "unit_id", referencedColumnName: "unitId"})
    unit: Unit;

    @ManyToOne(() => Goods, {createForeignKeyConstraints: false})
    @JoinColumn({name: "goods_id", referencedColumnName: "id"})
    goods: Goods;

    
}